package com.tduck.cloud.datav.service.charts;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.google.common.collect.Lists;
import com.tduck.cloud.datav.request.ChartDataRequest;
import com.tduck.cloud.datav.service.BaseChartDataService;
import com.tduck.cloud.datav.vo.BasicMapVO;
import com.tduck.cloud.report.data.params.GroupQueryParams;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 基础地图数据服务
 */
@Component
public class BasicMapChartDataService extends BaseChartDataService {


    @Override
    public Map<String, Object> getChartData(ChartDataRequest request) {
        List<GroupQueryParams.AggregateField> fieldList = request.getIndex().stream().map(item -> new GroupQueryParams.AggregateField(item.getId(), item.getAggregateType())).collect(Collectors.toList());
        GroupQueryParams params = new GroupQueryParams();
        params.setFilter(Lists.newArrayList(request.getFilter()));
        params.setAggregateFields(fieldList);
        params.setQueryObjName(request.getIndexName());
        params.setLimit(request.getLimit());
        List<Map> provinceGroupCount = dataSearchTemplate.getProvinceGroupCount(params);
        BasicMapVO basicMapVO = new BasicMapVO();
        if(CollUtil.isNotEmpty(provinceGroupCount)){
            basicMapVO.setMap(provinceGroupCount.stream().map(item -> new BasicMapVO.MapItem(MapUtil.getStr(item, "province"), MapUtil.getLong(item, "count"))).collect(Collectors.toList()));
        }
        return BeanUtil.beanToMap(basicMapVO);
    }
}
